//
//  YZSecurityVC.m
//  VET
//
//  Created by Lam BG on 2018/6/28.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZSecurityVC.h"

#import "YZUser.h"

#import "YZGradientBtn.h"

#import "YZSecurityVM.h"

// Category
#import "SVProgressHUD+Message.h"

@interface YZSecurityVC ()
@property (weak, nonatomic) IBOutlet UITextField *numberTF;
@property (weak, nonatomic) IBOutlet UIView *numberView;


@property (weak, nonatomic) IBOutlet UITextField *verCodeTF;
@property (weak, nonatomic) IBOutlet UIView *verCodeView;


@property (weak, nonatomic) IBOutlet YZGradientBtn *verCodeBtn;
@property (weak, nonatomic) IBOutlet YZGradientBtn *bindBtn;


@property (strong, nonatomic) YZSecurityVM *securityVM;

@end

@implementation YZSecurityVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // vc title
    self.title = @"安全账户手机绑定";
}

- (void)bindVM {
    
    RAC(self.securityVM, number) = self.numberTF.rac_textSignal;
    RAC(self.securityVM, verCode) = self.verCodeTF.rac_textSignal;
    
    @weakify(self)
    // 错误信息提示
    [[RACObserve(self.securityVM, numberWarningInfo) distinctUntilChanged] subscribeNext:^(NSString *msg) {
        
        if (msg.length > 0) {
            
            [SVProgressHUD yz_showMessage:msg];
        }
    }];
    
    
    
    // 验证码
    RAC(self.verCodeBtn, enabled) = RACObserve(self.securityVM, canVerCode);
    [[self.verCodeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {

        @strongify(self)
        [self.securityVM.verCodeCmd execute:nil];
    }];
    [[self.securityVM.verCodeCmd executionSignals] subscribeNext:^(RACSignal<id> *signal) {

        [signal subscribeNext:^(NSString *uid) {

            @strongify(self)
            [YZUser sharedInstance].uniqueId = uid;
            //            yingzi-6fa9c52d-65d9-46fe-a29d-7a396d55726a---721082

#if DEBUG
            if ([uid containsString:@"---"]) {

                NSString *verCode = [uid substringFromIndex:[uid rangeOfString:@"---"].location + 3];
                self.verCodeTF.text = verCode;
                self.securityVM.verCode = verCode;
            }
#endif
        } completed:^{
            
            @strongify(self)
            [self.verCodeBtn startCount:60];
        }];
    }];


    
    // 绑定按钮
    RAC(self.bindBtn, enabled) = self.securityVM.bindSignal;
    [[self.bindBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {

        @strongify(self)
        [self.securityVM.bindCmd execute:nil];
    }];
    [[self.securityVM.bindCmd executionSignals] subscribeNext:^(RACSignal<id> *signal) {

        [signal subscribeNext:^(NSString *uid) {

            @strongify(self)
            // 成功后，跳转到上传图片和信息
            UIStoryboard *personalStoryboard = [UIStoryboard storyboardWithName:@"YZLaunchSB" bundle:nil];
            UIViewController *vc = [personalStoryboard instantiateViewControllerWithIdentifier:@"YZAvatarVC"];
            [self.navigationController pushViewController:vc animated:YES];
        }];
    }];
}

#pragma mark - getter 懒加载

- (YZSecurityVM *)securityVM {
    
    if (_securityVM == nil) {
        
        _securityVM = [[YZSecurityVM alloc] init];
    }
    
    return _securityVM;
}


@end














